library(foreign)
OD<-read.csv("rawdata.csv")

##OD/"rawdata.csv" is GGW's original (unimputed) data with further variables added manually from the Database of Political Institutions dataset

OD[is.na(OD$execrlc)==TRUE,"execrlc"]<- -999
OD[is.na(OD$gov1rlc)==TRUE,"gov1rlc"]<- -999
## execrlc is the DPI's coding of the ideological orientation of the party holding the executive branch, and gov1rlc is the same for the largest party in the coalition controlling the legislative branch.   

## .inc means inclusive and treats non-ideological parties as centrist, .exc means exclusive and disregards these parties
zeros<-c(rep(0,nrow(OD)))
rightshift.inc<-zeros
leftshift.inc<-zeros
rightshift.exc<-zeros
leftshift.exc<-zeros

##Including legislative shifts
leftshift.inc.leg<-zeros
leftshift.exc.leg<-zeros
rightshift.inc.leg<-zeros
rightshift.exc.leg<-zeros

## Create leftshift.inc
for(i in 1:(nrow(OD)-1)){
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==1) & (OD[i+1,"execrlc"]==3)) leftshift.inc[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==1) & (OD[i+1,"execrlc"]==2)) leftshift.inc[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==2) & (OD[i+1,"execrlc"]==3)) leftshift.inc[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==0) & (OD[i+1,"execrlc"]==3)) leftshift.inc[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==1) & (OD[i+1,"execrlc"]==0)) leftshift.inc[i+1]=1
if(OD[i+1,"execrlc"]==-999) leftshift.inc[i+1]=NA
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==-999)) leftshift.inc[i+1]=NA
}
if(OD[1,"execrlc"]==-999) leftshift.inc[1]=NA
table(leftshift.inc)

## Create rightshift.inc
for(i in 1:(nrow(OD)-1)){
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==3) & (OD[i+1,"execrlc"]==1)) rightshift.inc[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==2) & (OD[i+1,"execrlc"]==1)) rightshift.inc[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==3) & (OD[i+1,"execrlc"]==2)) rightshift.inc[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==0) & (OD[i+1,"execrlc"]==1)) rightshift.inc[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==3) & (OD[i+1,"execrlc"]==0)) rightshift.inc[i+1]=1
if(OD[i+1,"execrlc"]==-999) rightshift.inc[i+1]=NA
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==-999)) rightshift.inc[i+1]=NA
}
if(OD[1,"execrlc"]==-999) rightshift.inc[1]=NA
table(rightshift.inc)

## Create leftshift.exc
for(i in 1:(nrow(OD)-1)){
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==1) & (OD[i+1,"execrlc"]==3)) leftshift.exc[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==1) & (OD[i+1,"execrlc"]==2)) leftshift.exc[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==2) & (OD[i+1,"execrlc"]==3)) leftshift.exc[i+1]=1
if(OD[i+1,"execrlc"]==-999) leftshift.exc[i+1]=NA
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==-999)) leftshift.exc[i+1]=NA
}
if(OD[1,"execrlc"]==-999) leftshift.exc[1]=NA

table(leftshift.exc)

## Create rightshift.exc
for(i in 1:(nrow(OD)-1)){
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==3) & (OD[i+1,"execrlc"]==1)) rightshift.exc[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==2) & (OD[i+1,"execrlc"]==1)) rightshift.exc[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==3) & (OD[i+1,"execrlc"]==2)) rightshift.exc[i+1]=1
if(OD[i+1,"execrlc"]==-999) rightshift.exc[i+1]=NA
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==-999)) rightshift.exc[i+1]=NA
}
if(OD[1,"execrlc"]==-999) rightshift.exc[1]=NA
table(rightshift.exc)

## Create leftshift.inc.leg
for(i in 1:(nrow(OD)-1)){
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==1) & (OD[i+1,"execrlc"]==3)) leftshift.inc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==1) & (OD[i+1,"execrlc"]==2)) leftshift.inc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==2) & (OD[i+1,"execrlc"]==3)) leftshift.inc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==0) & (OD[i+1,"execrlc"]==3)) leftshift.inc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==1) & (OD[i+1,"execrlc"]==0)) leftshift.inc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"gov1rlc"]==1) & (OD[i+1,"gov1rlc"]==3)) leftshift.inc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"gov1rlc"]==1) & (OD[i+1,"gov1rlc"]==2)) leftshift.inc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"gov1rlc"]==2) & (OD[i+1,"gov1rlc"]==3)) leftshift.inc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"gov1rlc"]==0) & (OD[i+1,"gov1rlc"]==3)) leftshift.inc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"gov1rlc"]==1) & (OD[i+1,"gov1rlc"]==0)) leftshift.inc.leg[i+1]=1
if(OD[i+1,"execrlc"]==-999) leftshift.inc.leg[i+1]=NA
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==-999)) leftshift.inc.leg[i+1]=NA
}
if(OD[1,"execrlc"]==-999) leftshift.inc.leg[1]=NA
table(leftshift.inc.leg)

## Create leftshift.exc.leg
for(i in 1:(nrow(OD)-1)){
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==1) & (OD[i+1,"execrlc"]==3)) leftshift.exc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==1) & (OD[i+1,"execrlc"]==2)) leftshift.exc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==2) & (OD[i+1,"execrlc"]==3)) leftshift.exc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"gov1rlc"]==1) & (OD[i+1,"gov1rlc"]==3)) leftshift.exc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"gov1rlc"]==1) & (OD[i+1,"gov1rlc"]==2)) leftshift.exc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"gov1rlc"]==2) & (OD[i+1,"gov1rlc"]==3)) leftshift.exc.leg[i+1]=1
if(OD[i+1,"execrlc"]==-999) leftshift.exc.leg[i+1]=NA
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==-999)) leftshift.exc.leg[i+1]=NA
}
if(OD[1,"execrlc"]==-999) leftshift.exc.leg[1]=NA
table(leftshift.exc.leg)

## Create rightshift.inc.leg
for(i in 1:(nrow(OD)-1)){
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==3) & (OD[i+1,"execrlc"]==1)) rightshift.inc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==2) & (OD[i+1,"execrlc"]==1)) rightshift.inc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==3) & (OD[i+1,"execrlc"]==2)) rightshift.inc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==0) & (OD[i+1,"execrlc"]==1)) rightshift.inc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==3) & (OD[i+1,"execrlc"]==0)) rightshift.inc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"gov1rlc"]==3) & (OD[i+1,"gov1rlc"]==1)) rightshift.inc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"gov1rlc"]==2) & (OD[i+1,"gov1rlc"]==1)) rightshift.inc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"gov1rlc"]==3) & (OD[i+1,"gov1rlc"]==2)) rightshift.inc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"gov1rlc"]==0) & (OD[i+1,"gov1rlc"]==1)) rightshift.inc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"gov1rlc"]==3) & (OD[i+1,"gov1rlc"]==0)) rightshift.inc.leg[i+1]=1
if(OD[i+1,"execrlc"]==-999) rightshift.inc.leg[i+1]=NA
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==-999)) rightshift.inc.leg[i+1]=NA
}
if(OD[1,"execrlc"]==-999) rightshift.inc.leg[1]=NA
table(rightshift.inc.leg)

## Create rightshift.exc.leg
for(i in 1:(nrow(OD)-1)){
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==3) & (OD[i+1,"execrlc"]==1)) rightshift.exc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==2) & (OD[i+1,"execrlc"]==1)) rightshift.exc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==3) & (OD[i+1,"execrlc"]==2)) rightshift.exc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"gov1rlc"]==3) & (OD[i+1,"gov1rlc"]==1)) rightshift.exc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"gov1rlc"]==2) & (OD[i+1,"gov1rlc"]==1)) rightshift.exc.leg[i+1]=1
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"gov1rlc"]==3) & (OD[i+1,"gov1rlc"]==2)) rightshift.exc.leg[i+1]=1
if(OD[i+1,"execrlc"]==-999) rightshift.exc.leg[i+1]=NA
if((OD[i,"imf_ccode"]==OD[i+1,"imf_ccode"])
& (OD[i,"execrlc"]==-999)) rightshift.exc.leg[i+1]=NA
}
if(OD[1,"execrlc"]==-999) rightshift.exc.leg[1]=NA
table(rightshift.exc.leg)

##This code creates shift_left_fixed (the main changes were done manually in fulldata.csv).  
library(foreign)
OD<-read.csv("rawdata.csv")

OD[is.na(OD$execrlc)==TRUE,"execrlc"]<- -999
OD[is.na(OD$shift_left_fixed)==TRUE,"shift_left_fixed"]<- -999
OD[OD$execrlc==-999,"shift_left_fixed"]<- -999
OD[OD$shift_left_fixed==-999,"shift_left_fixed"]<- NA
shift_left_fixed<-OD$shift_left_fixed

table(OD$shift_left)
table(OD$shift_left_fixed)

##This code creates leftrule, leftrule_exc, and leftrule_inc.  Leftrule is dichotomous, and the others are ordinal (1,2,3)
leftrule<-rep(0,nrow(OD))
OD<-cbind(OD,leftrule)
OD[OD$execrlc==3,"leftrule"]<- 1
OD[OD$execrlc==-999,"leftrule"]<- NA
leftrule<-OD$leftrule

leftrule.inc<-rep(0,nrow(OD))
OD<-cbind(OD,leftrule.inc)
OD[OD$execrlc==3,"leftrule.inc"]<- 3
OD[OD$execrlc==2,"leftrule.inc"]<- 2
OD[OD$execrlc==1,"leftrule.inc"]<- 1
OD[OD$execrlc==0,"leftrule.inc"]<- 2
OD[OD$execrlc==-999,"leftrule.inc"]<- NA
leftrule_inc<-OD$leftrule.inc

leftrule.exc<-rep(0,nrow(OD))
OD<-cbind(OD,leftrule.exc)
OD[OD$execrlc==3,"leftrule.exc"]<- 3
OD[OD$execrlc==2,"leftrule.exc"]<- 2
OD[OD$execrlc==1,"leftrule.exc"]<- 1
OD[OD$execrlc==0,"leftrule.exc"]<- NA
OD[OD$execrlc==-999,"leftrule.exc"]<- NA
leftrule_exc<-OD$leftrule.exc

#########################
shiftvars<-as.data.frame(cbind(leftshift.inc,rightshift.inc,leftshift.exc,
rightshift.exc,leftshift.inc.leg,rightshift.inc.leg,leftshift.exc.leg,
rightshift.exc.leg,shift_left_fixed,leftrule,leftrule.inc,leftrule.exc))

##Add the rest of the data (only country names will be retained)
shiftvarsOD<-cbind(shiftvars,OD)
##Drop years without shifts data (1967-1974)
checkdata<- subset(shiftvarsOD,year>1974)
##Drop Dominica due to missing domestic political data
checkdata<- subset(checkdata,imf_ccode!=30)
##Drop Hong Kong
checkdata<- subset(checkdata,imf_ccode!=52)
##Drop Netherlands Antilles
checkdata<- subset(checkdata,imf_ccode!=85)
##Drop St Vincent
checkdata<- subset(checkdata,imf_ccode!=103)
##Drop Sao Tome and Principe
checkdata<- subset(checkdata,imf_ccode!=104)
##Drop Seychelles
checkdata<- subset(checkdata,imf_ccode!=107)
##Drop Antigua and Barbuda
checkdata<- subset(checkdata,imf_ccode!=143)
##Drop Aruba
checkdata<- subset(checkdata,imf_ccode!=145)
##Drop Kiribati
checkdata<- subset(checkdata,imf_ccode!=162)
##Drop Marshall Islands
checkdata<- subset(checkdata,imf_ccode!=165)
##Drop Micronesia
checkdata<- subset(checkdata,imf_ccode!=166)
##Drop St Kitts and Nevis
checkdata<- subset(checkdata,imf_ccode!=174)
##Drop San Marino
checkdata<- subset(checkdata,imf_ccode!=175)
##Drop Tonga
checkdata<- subset(checkdata,imf_ccode!=180)

##Retain shift vars and country names
newvars<-checkdata[,1:(ncol(shiftvars)+1)]

##Add Variables to 5 Imputed Datasets (for Table 4)
out1<-read.dta("out_1.dta")
out2<-read.dta("out_2.dta")
out3<-read.dta("out_3.dta")
out4<-read.dta("out_4.dta")
out5<-read.dta("out_5.dta")

data1<-cbind(out1,newvars)
data2<-cbind(out2,newvars)
data3<-cbind(out3,newvars)
data4<-cbind(out4,newvars)
data5<-cbind(out5,newvars)

write.dta(data1,"data1.dta")
write.dta(data2,"data2.dta")
write.dta(data3,"data3.dta")
write.dta(data4,"data4.dta")
write.dta(data5,"data5.dta")

##Note: to complete the variable construction, the Stata code must be run on these files next.  They should then be saved as dataFD_1.dta and so forth.  

##Creating data for Table 2
library(foreign)
treaty1 <- read.dta("Treaty_mi_1.dta")
treaty2 <- read.dta("Treaty_mi_2.dta")
treaty3 <- read.dta("Treaty_mi_3.dta")
treaty4 <- read.dta("Treaty_mi_4.dta")
treaty5 <- read.dta("Treaty_mi_5.dta")

d1<-cbind(treaty1,shiftvars)
d2<-cbind(treaty2,shiftvars)
d3<-cbind(treaty3,shiftvars)
d4<-cbind(treaty4,shiftvars)
d5<-cbind(treaty5,shiftvars)

write.dta(d1,"treaty_1.dta")
write.dta(d2,"treaty_2.dta")
write.dta(d3,"treaty_3.dta")
write.dta(d4,"treaty_4.dta")
write.dta(d5,"treaty_5.dta")

##Note: to complete the variable construction, the Stata code must be run on these files next.  They should then be saved as Treaty_1.dta and so forth.  
